package psn.kiko.service.business.associated;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import psn.kiko.constant.DBConstant;
import psn.kiko.mapper.business.associated.InterfaceRoleMapper;
import psn.kiko.pojo.dto.InterfaceDTO;
import psn.kiko.pojo.entity.system.InterfaceEntity;
import psn.kiko.pojo.entity.system.InterfaceRoleEntity;
import psn.kiko.pojo.vo.InterfaceVO;

import java.util.ArrayList;
import java.util.List;

/**
 * interface_role中间表服务
 */
@Service
public class InterfaceRoleService{
	@Autowired
	InterfaceRoleMapper interfaceRoleMapper;
	
	/**
	 * 为一个角色批量分配接口
	 */
	public Integer allocateMultiInterfacesToOneRole(Integer roleId,List<Integer> interfaceIds){
		List<InterfaceRoleEntity> list = new ArrayList<>();
		for(Integer interfaceId: interfaceIds){
			list.add(new InterfaceRoleEntity(interfaceId,roleId,DBConstant.ENABLE));
		}
		return interfaceRoleMapper.batchInsert(list);
	}
	
	/**
	 * 将一个接口分配给一批角色
	 */
	public Integer allocateOneInterfaceToMultiRoles(Integer interfaceId,List<Integer> roleIds){
		List<InterfaceRoleEntity> list = new ArrayList<>();
		for(Integer roleId: roleIds){
			list.add(new InterfaceRoleEntity(interfaceId,roleId,DBConstant.ENABLE));
		}
		return interfaceRoleMapper.batchInsert(list);
	}
	
	/**
	 * 根据角色主键批量删除记录
	 */
	public Integer batchDeleteByRoleIds(List<Integer> roleIds){
		return interfaceRoleMapper.batchDelete(roleIds,null);
	}
	
	/**
	 * 根据接口主键批量删除记录
	 */
	public Integer batchDeleteByInterfaceIds(List<Integer> apiStableIds){
		return interfaceRoleMapper.batchDelete(null,apiStableIds);
	}
	
	/**
	 * 批量启用某一角色的一批接口
	 */
	public Integer batchEnableByRoleId(Integer roleId,List<Integer> apiStableIds){
		List<Integer> roleIds = new ArrayList<>(1);
		roleIds.add(roleId);
		return interfaceRoleMapper.batchUpdateStatus(roleIds,apiStableIds,DBConstant.ENABLE);
	}
	
	/**
	 * 批量禁用某一角色的一批接口
	 */
	public Integer batchDisableByRoleId(Integer roleId,List<Integer> apiStableIds){
		List<Integer> roleIds = new ArrayList<>(1);
		roleIds.add(roleId);
		return interfaceRoleMapper.batchUpdateStatus(roleIds,apiStableIds,DBConstant.DISABLE);
	}
	
	/**
	 * 允许一批角色访问某一个接口
	 */
	public Integer batchEnableRolesByInterfaceId(List<Integer> roleIds,Integer apiStableId){
		List<Integer> apiStableIds = new ArrayList<>(1);
		apiStableIds.add(apiStableId);
		return interfaceRoleMapper.batchUpdateStatus(roleIds,apiStableIds,DBConstant.ENABLE);
	}
	
	/**
	 * 禁止一批角色访问某一个接口
	 */
	public Integer batchDisableRolesByInterfaceId(List<Integer> roleIds,Integer apiStableId){
		List<Integer> apiStableIds = new ArrayList<>();
		apiStableIds.add(apiStableId);
		return interfaceRoleMapper.batchUpdateStatus(roleIds,apiStableIds,DBConstant.DISABLE);
	}
	
	/**
	 * 根据接口id和角色id获取
	 */
	public InterfaceRoleEntity getByApiIdAndRoleIds(Integer apiStableId,List<Integer> roleIds){
		return interfaceRoleMapper.selectByApiIdAndRoleIds(apiStableId,roleIds);
	}
	
	/**
	 * 获取某一角色 已分配的接口VO集合
	 */
	public List<InterfaceVO> getAllocatedApisByRoleId(int roleId,InterfaceDTO interfaceDTO){
		InterfaceEntity interfaceEntity = new InterfaceEntity();
		BeanUtils.copyProperties(interfaceDTO, interfaceEntity);
		return interfaceRoleMapper.selectAllocatedInterfaceOfRole(roleId,interfaceEntity);
	}
	
	/**
	 * 获取某一角色 还未分配的接口VO集合
	 */
	public List<InterfaceVO> getUnallocatedApisByRoleId(int roleId,InterfaceDTO interfaceDTO){
		InterfaceEntity interfaceEntity = new InterfaceEntity();
		BeanUtils.copyProperties(interfaceDTO, interfaceEntity);
		return interfaceRoleMapper.selectUnallocatedInterfaceOfRole(roleId,interfaceEntity);
	}
	
	/**
	 * 删除角色的一批接口
	 */
	public Integer batchDeleteByRoleIdAndApiIds(int roleId,List<Integer> apiStableIds){
		ArrayList<Integer> roleIds = new ArrayList<>();
		roleIds.add(roleId);
		return interfaceRoleMapper.batchDelete(roleIds,apiStableIds);
	}
}
